<template>
  <div>
    <!-- 面包屑导航区 -->
    <bread-nav>
      <span slot="c1">商品管理</span>
      <span slot="c2">商品列表</span>
    </bread-nav>

    <!-- 卡片视图区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getGoodsList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="3">
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- table 表格区域 -->
      <!-- stripe 隔行变色 -->
      <el-table :data="goodsList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column
          prop="goods_name"
          label="商品名称"
          width="660"
        ></el-table-column>
        <el-table-column
          width="110"
          prop="goods_price"
          label="商品价格(元)"
        ></el-table-column>
        <el-table-column prop="goods_weight" label="重量"></el-table-column>
        <el-table-column prop="add_time" label="时间" width="170">
          <template slot-scope="scope">{{
            scope.row.add_time | dateFormat
          }}</template>
        </el-table-column>
        <el-table-column label="操作" width="120">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="editGoodsById(scope.row.goods_id)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeById(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import BreadNav from 'components/content/nav/BreadNav.vue'
import { getGoodsList, deleteGoods } from 'network/goods.js'
export default {
  name: 'List',
  components: { BreadNav },
  data() {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5,
      },
      // 商品列表
      goodsList: [],
      // 总页数
      total: 0,
    }
  },
  created() {
    // 获取商品列表
    this.getGoodsList()
  },
  methods: {
    async getGoodsList() {
      const loading = this.$loading.service({
        lock: false, // 是否锁屏
        text: '拼命加载中...', // 加载动画的文字
        spinner: 'el-icon-loading', // 引入的loading图标
        background: 'rgba(0, 0, 0, 0.4)',
        body: true,
      })
      const { data: res } = await getGoodsList(this.queryInfo)
      this.$nextTick(() => {
        // 以服务的方式调用的 Loading 需要异步关闭
        loading.close()
      })
      if (res.meta.status !== 200) {
        this.$message.error('获取商品列表失败！')
        return
      }
      // this.$message.success('获取商品列表成功！')
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    // 点击页数按钮时触发函数
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    // 切换商品页面时触发函数
    handleCurrentChange(newPageNum) {
      this.queryInfo.pagenum = newPageNum
      this.getGoodsList()
    },
    // 移除商品函数
    async removeById(id) {
      const result = await this.$confirm(
        '此操作将永久删除该商品, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).catch((err) => err)

      if (result !== 'confirm') {
        this.$message({
          type: 'info',
          message: '已取消删除',
        })
        return
      }
      const { data: res } = await deleteGoods(id)
      if (res.meta.status !== 200) {
        this.$message({
          type: 'error',
          message: '删除失败!',
        })
        return
      }
      this.getGoodsList()
      this.$message({
        type: 'success',
        message: '删除成功!',
      })
    },
    // 添加商品按钮函数
    goAddPage() {
      this.$router.push('/goods/add')
    },
    // 编辑商品按钮函数
    editGoodsById(id) {
      this.$store.commit('editId', id)
      this.$router.push('/goods/edit')
    },
  },
}
</script>

<style scoped>
</style>